class Solution {
public:
    int maximumBeauty(vector<int>& nums, int k)
    {
        //1 2 4 6  left >= right - k   left <= right + k       right <= left + k <= right + k 
        int ans = 1;
        ranges::sort(nums);
        int right = 0;
        int left = 0;
        int n = nums.size();
        while (right < n)
        {
            while (left < right && nums[right] - nums[left] > 2 * k) left++;
            ans = ans > right - left + 1 ? ans : right - left + 1;
            right++;
        }
        return ans;
    }
};